package com.google.com.p.joat_wordfilter;

import org.bukkit.event.player.PlayerChatEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;

/**
 * 
 * the chat and logoff listener 
 *
 */
public class WordFilterPlayerListener extends PlayerListener{

	private WordFilter plugin;
	private BadWordFilter filter;
	
	/**
	 * constructor
	 * @param instance our parent
	 */
	public WordFilterPlayerListener(WordFilter instance) {
		plugin = instance;
		plugin.log.info("WordFilterPlayerListener initialized");
	}

	/**
	 * call to load the words for the listener
	 */
	public void load()
	{
		filter = new BadWordFilter(plugin.log,plugin.getDataFolder().getPath());
	}
	
	/**
	 * override to handle the chat message
	 * @param event 
	 */
	@Override
	public void onPlayerChat(PlayerChatEvent event) {

		String msg = event.getMessage();
		String update = filter.maskBadWords(msg);
		if ( update != null )
		{
			plugin.log.warning(event.getPlayer().getName() + " cursed! Original message: "+ msg);
			event.setMessage(update);
        	event.getPlayer().setFireTicks(10000);
		}
	}

	/**
	 * override for when a player leaves
	 * @param event 
	 */
	@Override
	public void onPlayerQuit(PlayerQuitEvent event) {
		// TODO Auto-generated method stub
		super.onPlayerQuit(event);
	}

	/**
	 * add a word command, passed through to impl
	 * @param string
	 */
	public void addWord(String string) {
		if ( filter.addWord(string) )
			filter.saveList();
	}
	
	/**
	 * remove a word command, passed through to impl
	 * @param string
	 */
	public void removeWord(String string) {
		if ( filter.removeWord(string) )
			filter.saveList();
	}
	 
}
